<template>
	<view class="warn">
		<view class="content">
			<view class="top">
				<xbd-color-title :image="require('@/static/image/notice/icon.svg')" :grade="from.grade">
					<template #grade>
						<text>小保当公司廉洁风险预警通知书</text>
					</template>
					<template #content>
						<view>
							<xbd-row mandatory='true' :title="'预警对象'">
								 <template v-slot:comments>
									 <view class="add" @click="selectObject">
										 点击添加
									 </view>
								 </template>
								<template>
									<view class="nameBorder">
										<view v-if="from.conference_participants.length === 0">
											请添加预警对象
										</view>
										<view class="name_text" v-else>
											<view class="name" v-for="(item,index) in from.conference_participants" :key="index">
												{{item.name}}
											</view>
										</view>
										
									</view>
								</template>
							</xbd-row>
							<xbd-row mandatory='true' :title="'风险等级'">
								<template>
									<view class="obj" @click="choiceVal">
										<text :style="{ color: gradeColor(from.grade) }">{{from.grade}}</text>
										<image src="@/static/image/bottom.svg" />
									</view>
								</template>
							</xbd-row>
							<xbd-row mandatory='true' :title="'预警事项'">
								<template>
									<view class="text">
										<textarea placeholder-style="color:#a0a7b8" v-model="from.matter" placeholder="请输入" maxlength="50" />
									</view>
								</template>
							</xbd-row>
							<xbd-row mandatory='true' :title="'预警意见'">
								<template>
									<view class="text">
										<textarea placeholder-style="color:#a0a7b8" auto-height maxlength='-1' v-model="from.opinion" placeholder="请输入" />
									</view>
								</template>
							</xbd-row>
							<xbd-row mandatory='true' :title="'防控措施'">
								<template>
									<view class="text">
										<textarea placeholder-style="color:#a0a7b8" auto-height v-model="from.deed" maxlength='-1' placeholder="请输入" />
									</view>
								</template>
							</xbd-row>
							<xbd-row mandatory='true' :title="'预警时间'">
								<template>
									<xbd-date-after fields="day" :date="from.time" :visible.sync="visible"
										@setDate="setDate">
										<template>
											<view class="obj" @click="visible = true">
												<text>{{ from.time }}</text>
											</view>
										</template>
									</xbd-date-after>
								</template>
							</xbd-row>
							<view class="tag" v-if="id==''">
								注：1.预警提交审核通过后，系统会在预警时间当日08:00自动向预警对象发送页面内的预警内容。<br>
								2.模板提交审核通过后，系统会每年在预警时间当日08:00自动向预警对象发送页面内的预警内容，再次发送无需审核。
							</view>
							<view class="tag" v-else>
								注：预警提交审核通过后，系统会在预警时间当日08:00自动向预警对象发送页面内的预警内容。
							</view>
						</view>

					</template>
				</xbd-color-title>
			</view>
		</view>
		<view v-if="id ==''" class="end">
			<view class="moduleBtn" @click="submit('mod')">模板提交</view>
			<view class="warnBtn" @click="submit('warn')">预警提交</view>
		</view>
		<xbd-button v-else text="预警提交" @clickBtn="submit('warn')"></xbd-button>
		<xbd-select :visible="gradeVisible" :infoList="gradeInfoList" :title="title" @update:visible="changeVisible"
			@update:carId="changeLabel" />
	</view>
</template>

<script>
	import {
		Prevention
	} from '@/api/index.js';
	export default {
		name: "warn",
		data() {
			return {
				id: '',
				flag: true,
				gradeVisible: false,
				visible: false,
				date: "日期选择",
				is_templete: false,
				value: "",
				from: {
					conference_participants:[],
					grade: "低",
					matter: "",
					opinion: "",
					deed: "",
					time: "日期选择",
				},
				title: "请选择",
				gradeInfoList: [{
						label: "低",
						value: "低",
					},
					{
						label: "中",
						value: "中",
					},
					{
						label: "高",
						value: "高",
					},
				],
			};
		},
		methods: {
			gradeColor(val) {
				let res = "";
				switch (val) {
					case "低":
						res = "#2E73FC";
						break;
					case "中":
						res = "orange";
						break;
					case "高":
						res = "red";
						break;
				}
				return res;
			},
			submit(val) {
				
				if (this.from.conference_participants.length === 0) {
					return this.$showToast('预警对象：请选择内容');
				} else if (this.from.matter == '') {
					return this.$showToast('预警事项：请填写内容');
				} else if (this.from.opinion == '') {
					return this.$showToast('预警意见：请填写内容');
				} else if (this.from.deed == '') {
					return this.$showToast('防控措施：请填写内容');
				} else if (this.from.time == '日期选择') {
					return this.$showToast('预警时间：请选择时间');
				}
				if (val === 'mod') {
					this.is_templete = true
				} else {
					this.is_templete = false
				}
				if(this.flag){
					this.flag = false
					this.create()
				}
				console.log(this.is_templete, 'this.is_templete')
			},
			async create() {
				let a=[]
				 this.from.conference_participants.forEach(item=>{
					console.log(item,'item')
					a.push(item.id)
				})
				const o = {
					target_id: a,
					level: this.from.grade,
					item: this.from.matter,
					is_templete: this.is_templete,
					time: this.from.time,
					measures: this.from.deed,
					view: this.from.opinion
				}
				const res = await Prevention.postWarnCreate(o)
				if (res.success) {
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/notice/record/record'
						});
					}, 1500)
				} else {
					this.flag = true
				}
				this.$showToast(res.msg);
				console.log(res)
			},
			choiceVal() {
				this.gradeVisible = true;
			},
			changeVisible(val) {
				this.gradeVisible = val;
			},
			changeLabel(val) {
				this.from.grade = val;
			},
			setDate(val) {
				console.log(val);
				this.from.time = val;
			},
			selectObject() {
				uni.navigateTo({
					url: `/pages/notice/warn/selectObj/selectObj?checked=${JSON.stringify(this.from.conference_participants)}`,
				});
			},
			async init() {
				const res = await Prevention.getWarnDetail(this.id)
				if(res.success){
					this.from.grade= res.data.level
					this.from.matter= res.data.item
					this.from.deed= res.data.measures
					this.from.opinion= res.data.view
				}
				console.log(res,'res')
				
			},
			participants(arr) {
							this.from.conference_participants = arr;
							console.log(arr,'arr')
						},
		},
		onLoad(res) {
			if (res.id) {
				this.id = res.id
				this.init() // console.log(res)
			}
			uni.$on('participants', this.participants);

		},
		onUnload() {
					uni.$off('participants');
				}
	};
</script>

<style lang="scss" scoped>
	@import "@/static/common.scss";
	.warn {
		@include compress;

		.content {
			.add{
				margin-top: -8rpx;
				margin-left: 40rpx;
				border-radius: 8rpx;
				font-size: 24rpx;
				padding: 8rpx 20rpx;
				background: linear-gradient(266deg,#2e73fc 3%, #1ea9fb 95%);
				color: #FFFFFF;
			}
			.nameBorder{
				margin-top: 20rpx;
				box-sizing: border-box;
				padding: 24rpx 30rpx;
				width: 100%;
				border: 2rpx solid #EBEEF5;
				font-size: 28rpx;
				font-weight: 400;
				color: #a0a7b8;
				.name_text{
					box-sizing: border-box;
					display: flex;
					flex-wrap: wrap;
				}
				.name{
					margin-bottom: 20rpx;
					color:  #2E73FC;
					margin-right: 20rpx;
					padding:8rpx 16rpx;
					border: 2rpx solid #2E73FC;
					border-radius: 8rpx;
				}
			}
			.tag {
				font-size: 24rpx;
				// font-family: PingFang SC, PingFang SC-Regular;
				font-weight: 400;
				text-align: left;
				color: #a0a7b8;
				line-height: 40rpx;
			}

			background-color: #fff;
			@include compress;

			.obj {
				width: 100%;
				border-bottom: 1rpx solid #ebeef5;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx 0;
				box-sizing: border-box;
				color: #a0a7b8;
				font-size: 30rpx;
				.time {
					transform: rotate(0deg) !important;
				}

				image {
					width: 30rpx;
					height: 30rpx;
					transform: rotate(-90deg);
				}
			}

			.text {
				width: 100%;
				padding: 20rpx 0 0 0;
				box-sizing: border-box;

				textarea {
					width: 100%;
					height: 160rpx;
					border: 1rpx solid #ebeef5;
					padding: 0;
					margin: 0;
					padding: 20rpx;
					box-sizing: border-box;
					font-size: 30rpx;
					color: #000623;
				}
			}
		}

		.end {
			display: flex;
			justify-content: space-between;
			margin-top: 50rpx;

			view {
				width: 316rpx;
				height: 88rpx;
				text-align: center;
				line-height: 88rpx;
				font-size: 32rpx;
				border-radius: 12rpx;
			}

			.moduleBtn {
				border: 1rpx solid #2a84e0;
				color: #2a84e0;
			}

			.warnBtn {
				background-color: #2a84e0;
				color: #fff;
			}
		}
	}
</style>
